<template>
  <div class="tablePageWarp">
    <div>
      <a-form layout="vertical" >
        <a-row :gutter="[16,0]" style="margin-bottom: 16px;">
          <!--软件版本  -->
          <a-col :md="6" :sm="24">
            <a-form-model-item :label="$t('软件版本')">
              <software-version-select
                mode="default"
                :query="{ecuModuleId:ecuId}"
                :time="time"
                :value="queryParam.version"
                @changeoption="(val) => {
                  queryParam.version = val; refreshData(true)}"
              >
              </software-version-select>
            </a-form-model-item>
          </a-col>
          <!-- 创建时间-->
          <a-col :md="6" :sm="24">
            <a-form-model-item :label="$t('上报时间')">
              <a-range-picker
                @change="selectDateRange"
                v-model="createTime"
                :show-time="{
                  hideDisabledOptions: true,
                  defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
                }"
                style="width: 100%"
                :placeholder="['上报时间-开始','上报时间-结束']"
              />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24" style="margin-top: 22px;">
            <div class="searchBtns">
              <a-button-group>
                <auth-button type="primary" @click="refreshData(true)">
                  <i class="iconfont icon-chazhao"></i>
                  {{ $t('inquiry') }}
                </auth-button>
                <a-button @click="handleClear">
                  <i class="iconfont icon-qingkong"></i>
                  {{ $t('clear') }}
                </a-button>
              </a-button-group>
            </div>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="commonTableSty">
      <s-table
        class="h100"
        ref="table"
        :columns="columns"
        :data="loadData"
        :pagination="false"
        :scrollX="900"
        :rowKey="(record, index) => index"
        size="default" >
        <template slot-scope="text" v-for="column in commonColumns" :slot="column.scopedSlots.customRender">
          <table-ellipsis :text="text || '-'" :key="column.key"></table-ellipsis>
        </template>
        <!-- 当前软件版本 -->
        <template slot="version" slot-scope="text, record">
          <span v-if=" text && record.changedItem && isnumber(record.changedItem, '1')" style="font-weight:bold">{{ text }}</span>
          <question-mark-tip
            v-if="text && record.changedItem && isnumber(record.changedItem, '1')"
            :tip-title="$t('与上次发生变化')"
            :popupContainer="getPopupContainer"
          ></question-mark-tip>
          <span v-else>{{ text || '-' }}</span>
          <span v-if="text==='7F22'">{{ '(消极应答)' }}</span>
        </template>
        <!-- 软件编码(车端上报) -->
        <template slot="softwareCode" slot-scope="text, record">
          <span v-if=" text && record.changedItem && isnumber(record.changedItem, '2')" style="font-weight:bold">{{ text }}</span>
          <question-mark-tip
            v-if="text && record.changedItem && isnumber(record.changedItem, '2')"
            :tip-title="$t('与上次发生变化')"
            :popupContainer="getPopupContainer"
          ></question-mark-tip>
          <span v-else>{{ text || '-' }}</span>
          <span v-if="text==='7F22'">{{ '(消极应答)' }}</span>
        </template>
        <!-- 硬件版本 -->
        <template slot="hardVersion" slot-scope="text, record">
          <span v-if=" text && record.changedItem && isnumber(record.changedItem, '3')" style="font-weight:bold">{{ text }}</span>
          <question-mark-tip
            v-if="text && record.changedItem && isnumber(record.changedItem, '3')"
            :tip-title="$t('与上次发生变化')"
            :popupContainer="getPopupContainer"
          ></question-mark-tip>
          <span v-else>{{ text || '-' }}</span>
          <span v-if="text==='7F22'">{{ '(消极应答)' }}</span>
        </template>
      </s-table>
    </div>
  </div>
</template>
<script>
import TableEllipsis from '@/components/Ellipsis/TableEllipsis'
import { STable } from '@/components'
import IsStandardConfigIcon from './IsStandardConfigIcon'
import AuthText from '@/components/AuthText'
import moment from 'moment'
import AuthButton from '@/components/AuthButton'
import { versionSyncHistoryColumns } from '@/views/vehicle/config/list'
import CustomModal from '@/components/CustomModal/index.vue'
import SoftwareVersionSelect from '@/views/components/SoftwareVersionSelect.vue'
import QuestionMarkTip from '@/views/components/QuestionMarkTip'
const initQueryParam = {
  // 软件版本
  version: undefined,
  // 上报时间开始
  startTimestamp: undefined,
  // 上报时间结束
  finishTimestamp: undefined
}
export default {
  components: {
    TableEllipsis,
    STable,
    IsStandardConfigIcon,
    AuthText,
    AuthButton,
    CustomModal,
    SoftwareVersionSelect,
    QuestionMarkTip
  },
  props: {
    vin: {
      type: String,
      default: ''
    },
    ecuId: {
      type: String,
      default: ''
    }
  },
  computed: {
    commonColumns () {
      return this.columns.filter(item => !item.special)
    }
  },
  created () {
  },
  data () {
    return {
      moment,
      columns: versionSyncHistoryColumns,
      queryParam: { ...initQueryParam },
      createTime: null,
      time: Date.now()
    }
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    getPopupContainer () {
      return document.body
    },
    isnumber (val, isVal) {
      return val.indexOf(isVal)!=-1
    },
    // 上报时间选择
    selectDateRange (date, dateString) {
      this.queryParam.startTimestamp = dateString[0]
      this.queryParam.finishTimestamp = dateString[1]
      this.refreshData(true);
    },
    handleClear () {
      this.queryParam = { ...initQueryParam }
      this.createTime = undefined
      this.refreshData(true)
    },
    // 刷新
    refreshData (isToFirstPage = false) {
      const vm = this
      vm.$nextTick(() => {
        this.$refs.table.refresh(isToFirstPage)
      })
    },
    loadData (parameter) {
      const vm = this
      // 如果是全部则不传值给后端
      const JnqueryParam = JSON.parse(JSON.stringify(vm.queryParam))
      // eslint-disable-next-line no-restricted-syntax
      for (const key in JnqueryParam) {
        if (JnqueryParam[key] === '') {
          JnqueryParam[key] = undefined
        }
      }
      this.tableData = []
      const apiPromise = vm.$api.getCarEcuHistory({ ...parameter, ...JnqueryParam, vin: this.vin, ecuId: this.ecuId })
      return new Promise((resolve, reject) => {
        return apiPromise.then(res => {
          if (res.data) {
            this.tableData = res.data
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
</script>
<style scoped lang="less">
 ::v-deep .ant-form-item-label {
  padding-bottom: 2px;
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
